<script>
	import Vue from 'vue'
	import {plat} from 'utils/common.js'
	import { mapState, mapMutations } from "vuex"
	export default {
		computed: {
			...mapState({
				musicPlayingKey: ({config}) => config.musicPlayingKey,
			}),
		},
		methods: {
			...mapMutations({
				SET_musicPlayingKey: "config/SET_musicPlayingKey",
				SET_curPlayTime: "config/SET_curPlayTime",
				SET_sliderValue: "config/SET_sliderValue",
			}),
		},
		onLaunch: function() {
			let audioPlayer = null;
			// let timer = null;
			// if(plat === 'H5'){
				audioPlayer = uni.createInnerAudioContext()
			// }else{
			// 	audioPlayer = uni.getBackgroundAudioManager()
			// }
			audioPlayer.selfPlay = (src) => {
				// return new Promise((resolve, reject) => {
					this.SET_musicPlayingKey(!this.musicPlayingKey);
						if(this.musicPlayingKey) {
							this.$au_player.play();
							this.$au_player.onTimeUpdate(() => {
								const { currentTime,duration} = this.$au_player;
								this.SET_curPlayTime(Math.floor(currentTime));
								this.SET_sliderValue(Math.floor(currentTime));
								//this.sliderValue = Math.floor(currentTime);
								//resolve(Math.floor(currentTime));
							})
							this.$au_player.onEnded(() => {
								this.SET_musicPlayingKey(false);
							})
							this.$au_player.onError((res) => {
								this.SET_musicPlayingKey(false);
								uni.showToast({
									title: res,
									icon: "none",
								});
							});
						} else {
							this.$au_player.pause();
						}
				// })
			}
			// //注册事件函数写在全局或者写在vuex（不然重复绑定，内存泄露）里都行，这里图方便挂着全局
			Vue.prototype.$au_player = audioPlayer;
		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		}
	}
</script>

<style lang="scss">
	/* 注意要写在第一行，同时给style标签加入lang="scss"属性 */
	@import "uview-ui/index.scss";
	@import url("static/css/iconfont.css");
	/*每个页面公共css */
	page {
		height: 100%;
		background-color: #f9f9f9;
	}
</style>
